package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Api(tags = "套餐相关接口")
@RequestMapping("/admin/setmeal")
@RestController
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @CacheEvict(value = "setmeal", key = "#setmealDTO.categoryId")
    @ApiOperation("新增套餐")
    @PostMapping
    public Result addSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增套餐，套餐信息：{}", setmealDTO);
        setmealService.addSetmeal(setmealDTO);
        return Result.success();
    }

    @ApiOperation("套餐分页")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("套餐分页查询，参数：{}", setmealPageQueryDTO);
        PageResult page = setmealService.page(setmealPageQueryDTO);
        return Result.success(page);
    }

    @CacheEvict(value = "setmeal", allEntries = true)
    @ApiOperation("停售/起售套餐")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, Long id) {
        log.info("停售/起售套餐，状态：{}，套餐id：{}", status, id);
        setmealService.updateStatus(status, id);
        return Result.success();
    }

    @CacheEvict(value = "setmeal", allEntries = true)
    @ApiOperation("删除套餐")
    @DeleteMapping
    public Result deleteSetmeal(@RequestParam List<Long> ids) {
        log.info("删除套餐，套餐id：{}", ids);
        setmealService.deleteSetmeal(ids);
        return Result.success();
    }

    @ApiOperation("查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO> selectSetmeal(@PathVariable Long id) {
        log.info("查询套餐，套餐id：{}", id);
        SetmealVO setmealVO = setmealService.selectSetmeal(id);
        return Result.success(setmealVO);
    }

    @CacheEvict(value = "setmeal", allEntries = true)
    @ApiOperation("修改套餐")
    @PutMapping
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐，套餐信息：{}", setmealDTO);
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }
}
